#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QGraphicsView>
#include <QtGui/QWidget>
#include <QtGui/QMessageBox>
#include "uiview.h"
#include "mainscene.h"
#include "station.h"
#include "railway.h"
#include "cgraphicsview.h"

#define RADIUS 10

/********************************
* Add the station step by step
* 1. Select the toolbar button (checked)
* 2. Click on screen
* 3. Check if not already occupied point
* 4. Show the dialog box with name selection of station
* 5. Add the station to list
* 6. Uncheck the toolbar button
******************************/

CGraphicsView::CGraphicsView(QWidget * parent):
	        QGraphicsView(parent),
			m_scene(new CMainScene())
{ 
	setEnabled(true);
	m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
	QRect rect(this->viewport()->geometry());
	setSceneRect(0, 0, 998, 698);
	m_scene->setSceneRect(0, 0, 998, 698);
	//m_scene->setSceneRect(this->rect());
    setScene(m_scene);
    setCacheMode(CacheBackground);
    setViewportUpdateMode(BoundingRectViewportUpdate);
    setRenderHint(QPainter::Antialiasing);
    setTransformationAnchor(AnchorUnderMouse);
    //scale(qreal(0.8), qreal(0.8));
	//scale(1, -1);
    //setMinimumSize(400, 400);
    setWindowTitle(tr("Train Simulation"));
	setSlots();
};

void CGraphicsView::mousePressEvent ( QMouseEvent * mouseEvent )
{
	QSize viewport_size = viewport()->size();
	QPointF pt = mapToScene(mouseEvent->pos());
	QGraphicsView::mousePressEvent(mouseEvent);
}

void CGraphicsView::mouseReleaseEvent(QMouseEvent * mouseEvent)
{
	QGraphicsView::mouseReleaseEvent(mouseEvent);
}

void CGraphicsView::mouseMoveEvent(QMouseEvent * mouseEvent)
{
	QGraphicsView::mouseMoveEvent(mouseEvent);
}

void CGraphicsView::addStation(const QPoint & point)
{
	//TODO:Show the dialogbox with questionmark for a station name
	//QRect rect(point.x()-RADIUS,point.y()-RADIUS,point.x()+RADIUS,point.y()+RADIUS);
	//this->invalidateScene();
	//this->show();
	//drawStations();
}

void CGraphicsView::drawStations()
{
	/*
	QPainter painter(viewport());
	painter.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
	stationiterator it = m_railway.begin();
	while(it != m_railway.end())
	{
		CStation * st = *it;
		QPoint pt(st->x(), st->y());
		painter.drawEllipse(pt, RADIUS,RADIUS);
		it++;
	}*/
}

void CGraphicsView::paintEvent(QPaintEvent * paintEvent)
{
	QGraphicsView::paintEvent(paintEvent);
}

void CGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
{

}

void CGraphicsView::resizeEvent(QResizeEvent * paintEvent)
{

}

void CGraphicsView::setSlots()
{
	
}

void CGraphicsView::action_addstation(bool checked)
{
	if(checked)
	{
		m_scene->setDrawType(station);
		emit valueChanged(1);
	}else{
		if(m_scene->getDrawType() == station) 
			m_scene->setDrawType(unset);
	}
}

void CGraphicsView::action_addconnection(bool checked)
{
	if(checked)
	{
		m_scene->setDrawType(connection);
		emit valueChanged(2);
	}else{
		if(m_scene->getDrawType() == connection) 
			m_scene->setDrawType(unset);
	}
}

void CGraphicsView::action_addtrain(bool checked)
{
	if(checked)
	{
		//QMessageBox::about(0,"Action received","Action");
		m_scene->setDrawType(train);
		emit valueChanged(3);
	}else{
		if(m_scene->getDrawType() == train) 
			m_scene->setDrawType(unset);
	}
}